package com.rico.kok.controller.post;

import com.rico.kok.entity.Post;
import com.rico.kok.entity.User;
import com.rico.kok.service.interf.LikeService;
import com.rico.kok.service.interf.PostService;
import com.rico.kok.tool.MessageUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class LikeController {

    @Autowired
    LikeService likeService;

    //点赞和取消点赞
    @RequiresRoles("user")
    @RequestMapping("/postLike.htm")
    @ResponseBody
    public String like(@RequestParam("id") String likePostId, HttpSession session) {
        Post post = new Post();
        int userId = ((User) session.getAttribute("user")).getUserId();
        int i = Integer.parseInt(likePostId);
        post.setPostId(i);
        post.setUserId(userId);
        Integer like = likeService.isLike(post);
        if (like == null) {
            likeService.likePost(post);
            return "true";
        }else {
            likeService.disLike(post);
            return "false";
        }
    }
}
